package com.org.es5.api.controller;

import com.org.es5.api.entity.CmsDocument;
import com.org.es5.support.elasticserach.EsResult;
import com.org.es5.support.elasticserach.EsSearch;
import com.org.platform.model.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class CmsDocumentController {


    @RequestMapping("getCmsDocumentData")
    public ResultModel<EsResult<CmsDocument>> getCmsDocumentData() throws Exception {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        // 过滤客户类型
        BoolQueryBuilder custBuilder = QueryBuilders.boolQuery();
        QueryBuilder defaultCustTypeBuilder = QueryBuilders.termQuery("cust_type_array.keyword", "-1");
        custBuilder.should(defaultCustTypeBuilder);
        QueryBuilder custTypeBuilder = QueryBuilders.termQuery("cust_type_array.keyword", "3");
        custBuilder.should(custTypeBuilder);
        queryBuilder.must(custBuilder);
        // 过滤客户区域
        BoolQueryBuilder areaBuilder = QueryBuilders.boolQuery();
        QueryBuilder defaultAreaCodeBuilder = QueryBuilders.termQuery("area_code_array.keyword", "-1");
        areaBuilder.should(defaultAreaCodeBuilder);
        QueryBuilder areaCodeBuilder = QueryBuilders.termQuery("area_code_array.keyword", "100083");
        areaBuilder.should(areaCodeBuilder);
        queryBuilder.must(areaBuilder);
        searchSourceBuilder.query(queryBuilder);
//        EsSearch.setShowLog(true);
        EsResult<CmsDocument> esResult = EsSearch.getEsResult(searchSourceBuilder, 1, 10, "cms", "document",CmsDocument.class);
        return ResultModel.newSuccess(esResult);
    }

    @RequestMapping("initCmsDocumentData")
    public ResultModel<CmsDocument> initCmsDocumentData() throws Exception {
        long now = System.currentTimeMillis();
        CmsDocument cmsDocument = new CmsDocument();
        cmsDocument.setDocumentId(0L);
        cmsDocument.setDocumentTitle("0516测试文档标题");
        cmsDocument.setDocumentClassify("C0");
        cmsDocument.setDocumentSubClassify("CS0");
        cmsDocument.setIsShowFront(1);
        cmsDocument.setIsSelectObject(1);
        String[] areaCodeArray = new String[1];
        areaCodeArray[0] = "-1";
        cmsDocument.setAreaCodeArray(areaCodeArray);
        String[] custTypeArray = new String[1];
        custTypeArray[0] = "-1";
        cmsDocument.setCustTypeArray(custTypeArray);
        cmsDocument.setUpdateTime(String.valueOf(now));
        cmsDocument.setUpdateTimeLong(now);
        EsSearch.updateIndexDoc("cms", "document", cmsDocument);
        cmsDocument = new CmsDocument();
        cmsDocument.setDocumentId(1L);
        cmsDocument.setDocumentTitle("测试文档标题");
        cmsDocument.setDocumentClassify("C1");
        cmsDocument.setDocumentSubClassify("CS1");
        cmsDocument.setIsShowFront(1);
        cmsDocument.setIsSelectObject(1);
        int count = 0;
        areaCodeArray = new String[5000];
        for (int i = 100000; i < 105000; i++) {
            String areaCode = String.valueOf(i);
            areaCodeArray[count] = areaCode;
            count++;
        }
        cmsDocument.setAreaCodeArray(areaCodeArray);
        count = 0;
        custTypeArray = new String[10];
        for (int i = 1; i <= 10; i++) {
            String custType = String.valueOf(i);
            custTypeArray[count] = custType;
            count++;
        }
        cmsDocument.setCustTypeArray(custTypeArray);
        cmsDocument.setUpdateTime(String.valueOf(now));
        cmsDocument.setUpdateTimeLong(now);
        EsSearch.updateIndexDoc("cms", "document", cmsDocument);
        cmsDocument = new CmsDocument();
        cmsDocument.setDocumentId(2L);
        cmsDocument.setDocumentTitle("0515测试");
        cmsDocument.setDocumentClassify("C2");
        cmsDocument.setDocumentSubClassify("CS2");
        cmsDocument.setIsShowFront(1);
        cmsDocument.setIsSelectObject(1);
        areaCodeArray = new String[5];
        areaCodeArray[0] = "-1";
        areaCodeArray[1] = "100051";
        areaCodeArray[2] = "100067";
        areaCodeArray[3] = "100083";
        areaCodeArray[4] = "100096";
        cmsDocument.setAreaCodeArray(areaCodeArray);
        custTypeArray = new String[3];
        custTypeArray[0] = "-1";
        custTypeArray[1] = "3";
        custTypeArray[2] = "7";
        cmsDocument.setCustTypeArray(custTypeArray);
        cmsDocument.setUpdateTime(String.valueOf(now));
        cmsDocument.setUpdateTimeLong(now);
        boolean result = EsSearch.updateIndexDoc("cms", "document", cmsDocument);
        log.info("result:{}", result);
        return ResultModel.newSuccess(cmsDocument);
    }

}
